import time
import threading
import schedule


def job1():
    print("job1")


def job2(id):
    print(f"job2 {id}")


# 添加任务
def add_job(func, interval, tag, param=None):
    if func == job1:
        schedule.every(interval).seconds.do(func).tag(tag)
    else:
        schedule.every(interval).seconds.do(func, param).tag(tag)


def del_job(tag):
    for job in schedule.jobs:
        # 通过标签 删除任务
        if tag in job.tags:
            schedule.cancel_job(job)

def main():
    while True:
        schedule.run_pending()
        time.sleep(1)


if __name__ == "__main__":
    threading.Thread(target=main, daemon=True).start()
    help_str = """
1 添加任务job1 "tag1" -- a 删除任务标签 "tag1"
2 添加任务job2 "tag2" -- b 删除任务标签 "tag2"
3 添加任务job2 "tag2","tag3" -- c 删除任务标签 "tag3"
"""
    while True:
        print(help_str)
        cmd = input("请输入命令：")
        if cmd == "1":
            add_job(job1, 2, "tag1")
        elif cmd == "2":
            add_job(job2,3,"tag2","tag2",)
        elif cmd == "3":
            # tag = "tag2","tag3"
            # add_job(job2, 5, tag, "tag3")
            schedule.every(5).seconds.do(job2, "tag3").tag("tag2","tag3")
        elif cmd == "a":
            del_job("tag1")
        elif cmd == "b":
            del_job("tag2")
        elif cmd == "c":
            del_job("tag3")
